home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-08-31 | 12.0 KB | 525 lines | [TEXT/MMCC] |
- /****
- * CNeoStream.cp
- *
- * C++ implementation for NeoAccess abstract stream class.
- *
- * Copyright © Neologic Systems 1992-1994. All rights reserved
- *
- ****/
-
- #include "NeoTypes.h"
- #include CNeoStreamH
- #include CNeoBlobH
-
- /* ****************************************************************** */
- /** Instance Methods **/
- /* ****************************************************************** */
- /*
- * Initialize the stream's mark and length values.
- */
- #pragma segment NeoStream
- CNeoStream::CNeoStream(void)
- {
- fMark = 0;
- fLength = 0;
- }
-
- /*
- * A virtual destructor is defined just so that its subclass's
- * destructors are also virtual
- */
- #pragma segment NeoStream
- CNeoStream::~CNeoStream(void)
- {
- }
-
- /* ****************************************************************** */
- /** Access Methods **/
- /* ****************************************************************** */
- /*
- * This method needs to be overridden by the environment-specific stream subclasses.
- * Only this subclass knows how to actually close the stream.
- */
- #pragma segment NeoClose
- void CNeoStream::close(void)
- {
- // Null Method
- }
-
- /*
- * Some types of streams may use some form of data caching mechanism. This
- * method is used to insure that any buffered data committed.
- */
- #pragma segment NeoStream
- void CNeoStream::flush(const Boolean aEntireStream)
- {
- NeoUsed(aEntireStream);
-
- // Null Method
- }
-
- /*
- * Some streams may support the concept of a stream creator value. This
- * method is called to obtain the creator value of this stream.
- */
- #pragma segment NeoStream
- OSType CNeoStream::getCreator(void) const
- {
- return 0;
- }
-
- /*
- * Some streams may need to track the overall length of the data it contains.
- * This method is called to obtain the length of this stream.
- */
- #pragma segment NeoStream
- long CNeoStream::getLength(void) const
- {
- return fLength;
- }
-
- /*
- * A mark is a location relative to the beginning of a stream's data which
- * is where the next read or write is to occur. This method is called to
- * obtain the stream's mark value.
- */
- #pragma segment NeoStream
- NeoMark CNeoStream::getMark(void) const
- {
- return fMark;
- }
-
- /*
- * Return the type value for this stream.
- */
- #pragma segment NeoStream
- OSType CNeoStream::getType(void) const
- {
- return 0;
- }
-
- /*
- * Some siks may support the concept of whether it is open or closed. This
- * method may be called to determine whether the stream has been opened.
- */
- #pragma segment NeoStream
- Boolean CNeoStream::isOpen(void) const
- {
- return TRUE; // Assume that if the stream exists, then it is open
- }
-
- /*
- * Some streams may support the concept of a stream creator value. This
- * method is called to set the creator value of this stream.
- */
- #pragma segment NeoStream
- void CNeoStream::setCreator(const OSType aCreator)
- {
- NeoUsed(aCreator);
-
- // Null Method
- }
-
- /*
- * Some streams may need to track the overall length of the data it contains.
- * This method is called to set the length value of this stream.
- */
- #pragma segment NeoStream
- void CNeoStream::setLength(const long aLength, const Boolean aReallySet)
- {
- NeoUsed(aReallySet);
-
- fLength = aLength;
- }
-
- /*
- * SetMark sets the position of the mark in the data fork, which
- * specifies the next byte to read or write.
- */
- #pragma segment NeoInfo
- void CNeoStream::setMark(const NeoMark aMark)
- {
- NeoAssert(aMark <= fLength);
- fMark = aMark;
- }
-
- /*
- * Set the type value for this stream.
- */
- #pragma segment NeoStream
- void CNeoStream::setType(const OSType aType)
- {
- NeoUsed(aType);
-
- NeoAssert(FALSE);
- }
-
- /* ****************************************************************** */
- /** Structuring Methods **/
- /* ****************************************************************** */
- /*
- * Some streams may support the concept of containing a list of items.
- * This method is called to open a list for either reading or writing.
- */
- #pragma segment NeoStream
- void CNeoStream::closeList(void)
- {
- // Null Method
- }
-
- /*
- * Some streams may support the concept of containing a list of items.
- * This method is called to close a list after either reading or writing.
- */
- #pragma segment NeoStream
- void CNeoStream::openList(const NeoTag aTag)
- {
- NeoUsed(aTag);
-
- // Null Method
- }
-
- /* ****************************************************************** */
- /** I/O Methods **/
- /* ****************************************************************** */
- /*
- * Read the specified number of bytes from the stream into the buffer.
- */
- #pragma segment NeoStream
- void CNeoStream::readBits(void *aBits, const short aCount, const NeoTag aTag)
- {
- readChunk(aBits, aCount, aTag);
- }
-
- /*
- * Position the stream to the given mark then allocate a memory block of the
- * given length then read that number of bytes into the block.
- */
- #pragma segment NeoStream
- NeoBlob CNeoStream::readBlob(const NeoMark aMark, long &aLength, const NeoTag aTag)
- {
- NeoBlob blob;
- NeoMark oldMark;
-
- NeoUsed(aTag);
-
- if (aLength) {
- blob = NeoBlobCacheMalloc(aLength);
- NeoFailNil(blob);
- NEOTRY {
- oldMark = getMark();
- setMark(aMark);
- #ifdef qNeoMacintosh
- HLock((Handle)blob);
- #endif
- readChunk(NeoBlobGetPtr(blob), aLength);
- #ifdef qNeoMacintosh
- HUnlock((Handle)blob);
- #endif
- setMark(oldMark);
- }
- NEOCATCH {
- NeoBlobCacheFree(blob, aLength);
- }
- NEOENDTRY;
- }
- else {
- blob = nil;
- aLength = 0;
- }
-
- return blob;
- }
-
- /*
- * Read a single byte of data and treat it as a signed character.
- */
- #pragma segment NeoRead
- char CNeoStream::readChar(const NeoTag aTag)
- {
- char value;
-
- readChunk(&value, sizeof(value), aTag);
-
- return value;
- }
-
- /*
- * Read a double floating point value from the stream into the buffer.
- */
- #pragma segment NeoStream
- NeoDouble CNeoStream::readDouble(const NeoTag aTag)
- {
- NeoDouble value = 0;
-
- readChunk(&value, sizeof(value), aTag);
-
- return value;
- }
-
- /*
- * Read a floating point value from the stream into the buffer.
- */
- #pragma segment NeoStream
- NeoFloat CNeoStream::readFloat(const NeoTag aTag)
- {
- NeoFloat value;
-
- readChunk(&value, sizeof(value), aTag);
-
- return value;
- }
-
- /*
- * Read a long integer from the stream into the buffer.
- */
- #pragma segment NeoStream
- long CNeoStream::readLong(const NeoTag aTag)
- {
- long value = 0;
-
- readChunk(&value, sizeof(long), aTag);
-
- return value;
- }
-
- /*
- * Read a long double floating point value from the stream into the buffer.
- */
- #pragma segment NeoStream
- NeoLongDouble CNeoStream::readLongDouble(const NeoTag aTag)
- {
- NeoLongDouble value = 0;
-
- readChunk(&value, sizeof(value), aTag);
-
- return value;
- }
-
- /*
- * Read a byte from the stream and return Boolean indicating whether that byte is 0.
- */
- #pragma segment NeoStream
- Boolean CNeoStream::readBoolean(const NeoTag aTag)
- {
- char buffer;
-
- readChunk(&buffer, 1, aTag);
- return (Boolean)(buffer != 0);
- }
-
- /*
- * Read a native string from the stream into the buffer. The universal format
- * of native strings is a one byte length followed by up to 255 characters
- * of the string.
- */
- #pragma segment NeoStream
- void CNeoStream::readNativeString(CNeoString &aString, const long aMaxLength, const NeoTag aTag)
- {
- aString[0] = 0;
- #ifdef qNeoMacintosh
- readChunk((unsigned char *)aString, (aMaxLength ? aMaxLength : 255), aTag);
- #else
- char buffer[256];
-
- NeoAssert(aMaxLength <= 255);
- readChunk(buffer, (aMaxLength ? aMaxLength : 255), aTag);
- NeoAssert(buffer[0] < aMaxLength);
- NeoBlockMove(buffer+1, aString, buffer[0]);
- ((char *)aString)[buffer[0]] = 0;
- #endif
- }
-
- /*
- * The process of reading an object from a stream sometimes needs to begin
- * by first configuring or otherwise preparing the stream. The readObject
- * method is overridden by those streams to perform these preamble tasks.
- */
- #pragma segment NeoStream
- void CNeoStream::readObject(CNeoPersist *aObject, const NeoTag aTag)
- {
- NeoUsed(aObject);
- NeoUsed(aTag);
-
- // Null Method
- }
-
- /*
- * Read a short integer from the stream into the buffer.
- */
- #pragma segment NeoStream
- short CNeoStream::readShort(const NeoTag aTag)
- {
- short value = 0;
-
- readChunk(&value, sizeof(short), aTag);
-
- return value;
- }
-
- /*
- * Application specific clients of a stream can sometimes store and retrieve a
- * variable length, null terminated string in a stream. This method assumes
- * that the last character read is a terminating null.
- */
- #pragma segment NeoStream
- void CNeoStream::readString(void *aBuffer, const long aLength, const NeoTag aTag)
- {
- ((char *)aBuffer)[0] = 0;
- readChunk(aBuffer, aLength, aTag);
- }
-
- /*
- * Write the specified number of bytes from the buffer to the stream.
- */
- #pragma segment NeoStream
- void CNeoStream::writeBits(const void *aBits, const short aCount, const NeoTag aTag)
- {
- writeChunk(aBits, aCount, aTag);
- }
-
- /*
- * Position the stream to the given mark then write that number of
- * bytes to the stream.
- */
- #pragma segment NeoStream
- void CNeoStream::writeBlob(NeoBlob aBlob, const NeoMark aMark, const long aLength, const NeoTag aTag)
- {
- NeoMark oldMark;
-
- if (aBlob) {
- oldMark = getMark();
- setMark(aMark);
- #ifdef qNeoMacintosh
- HLock((Handle)aBlob);
- #endif
- writeChunk(NeoBlobGetPtr(aBlob), aLength, aTag);
- #ifdef qNeoMacintosh
- HUnlock((Handle)aBlob);
- #endif
- setMark(oldMark);
- }
- }
-
- /*
- * Write a 0 byte to the stream if aBool == FALSE.
- * Write a non-0 byte to the stream otherwise.
- */
- #pragma segment NeoStream
- void CNeoStream::writeBoolean(const Boolean aBool, const NeoTag aTag)
- {
- unsigned char buffer;
-
- if (aBool)
- buffer = 0xFF;
- else
- buffer = 0;
- writeChunk(&buffer, 1, aTag);
- }
-
- /*
- * Write a single byte of data and treat it as a signed character.
- */
- #pragma segment NeoRead
- void CNeoStream::writeChar(const char aValue, const NeoTag aTag)
- {
- writeChunk(&aValue, sizeof(aValue), aTag);
- }
-
- /*
- * Write a double floating point value to the stream.
- */
- #pragma segment NeoStream
- void CNeoStream::writeDouble(const NeoDouble &aValue, const NeoTag aTag)
- {
- writeChunk(&aValue, sizeof(aValue), aTag);
- }
-
- /*
- * Write a floating point value to the stream.
- */
- #pragma segment NeoStream
- void CNeoStream::writeFloat(const NeoFloat &aValue, const NeoTag aTag)
- {
- writeChunk(&aValue, sizeof(aValue), aTag);
- }
-
- /*
- * Write a long integer to the stream.
- */
- #pragma segment NeoStream
- void CNeoStream::writeLong(const long aValue, const NeoTag aTag)
- {
- writeChunk((const void *)&aValue, sizeof(long), aTag);
- }
-
- /*
- * Write a long double floating point value to the stream.
- */
- #pragma segment NeoStream
- void CNeoStream::writeLongDouble(const NeoLongDouble &aValue, const NeoTag aTag)
- {
- writeChunk(&aValue, sizeof(aValue), aTag);
- }
-
- /*
- * Write a native string to the stream. The universal format of native
- * strings is a one byte length followed by up to 255 characters of the
- * string.
- */
- #pragma segment NeoStream
- void CNeoStream::writeNativeString(const CNeoString &aString, const long aMaxLength, const NeoTag aTag)
- {
- #ifdef qNeoMacintosh
- writeChunk((unsigned char *)aString, aMaxLength, aTag);
- #else
- char buffer[256];
-
- buffer[0] = aString.getLength();
-
- NeoAssert(aMaxLength <= 255);
- if (buffer[0] > aMaxLength -1)
- buffer[0] = aMaxLength -1;
- NeoBlockMove(aString, buffer+1, buffer[0]);
- writeChunk(buffer, aMaxLength, aTag);
- #endif
- }
-
- /*
- * The process of writing an object to a stream sometimes involves first
- * configuring or otherwise preparing the stream. The writeObject method
- * overridden by those streams to perform these preamble tasks.
- */
- #pragma segment NeoStream
- void CNeoStream::writeObject(CNeoPersist *aObject, const NeoTag aTag)
- {
- NeoUsed(aObject);
- NeoUsed(aTag);
-
- // Null Method
- }
-
- /*
- * Write a short integer to the stream.
- */
- #pragma segment NeoStream
- void CNeoStream::writeShort(const short aValue, const NeoTag aTag)
- {
- writeChunk((const void *)&aValue, sizeof(short), aTag);
- }
-
- /*
- * Application specific clients of a stream can sometimes store and retrieve a
- * variable length, null terminated string in a stream. This method assumes
- * that the last character read is a terminating null.
- *
- * Note: I know I'm writing out aMaxLength bytes when the string may in
- * fact be shorter. It has to be that way, believe me!
- */
- #pragma segment NeoStream
- void CNeoStream::writeString(const void *aString, const long aMaxLength, const NeoTag aTag)
- {
- writeChunk(aString, aMaxLength, aTag);
- }
-
-